﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CaifenWeb.UtilityCls;
using CF.Data.Entity.FrmMemberEntity;
using CF.Member.DAL.Member;
using Ext.Net.MVC;

namespace CaifenWeb.Areas.BgMgr.Controllers
{
     [BgAuthorize]
    public class LevelConfigController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            return View();
        }

        public ActionResult Edit(int lid)
        {
            ViewData["LID"] = lid;
            return View();
        }

        public AjaxStoreResult GetAllLevelCfg(int? start, int? limit)
        {
            FM_LevelConfigDAL ldal = new FM_LevelConfigDAL();
            caifendbEntities context = new caifendbEntities();
            int startNum = start ?? 0;
            int limitNum = limit ?? 30;
            List<FM_LevelConfig> lvlist = ldal.GetAllLevelCfg(startNum, limitNum);
            return new AjaxStoreResult(lvlist, lvlist.Count);
        }

        [HttpPost]
        public AjaxFormResult AddLevelCfg(FM_LevelConfig lvlCfg)
        {
            AjaxFormResult response = new AjaxFormResult();
            try
            {
                FM_LevelConfigDAL ldal = new FM_LevelConfigDAL();
                ldal.Add(lvlCfg);
                response.Success = true;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams["msg"] = ex.Message;
            }
            return response;
        }

        [HttpPost]
        public AjaxFormResult UpdateLevelCfg(FM_LevelConfig lvlCfg)
        {
            AjaxFormResult response = new AjaxFormResult();
            try
            {
                FM_LevelConfigDAL ldal = new FM_LevelConfigDAL();
                if (ldal.Update(lvlCfg.LID, lvlCfg))
                {
                    response.Success = true;
                }
                else
                {
                    response.Success = false;
                    response.ExtraParams["msg"] = "找不到数据。";
                }
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams["msg"] = ex.Message;
            }
            return response;
        }

        [HttpPost]
        public AjaxFormResult RemoveLevelCfg(int lid)
        {
            AjaxFormResult response = new AjaxFormResult();
            FM_LevelConfigDAL ldal = new FM_LevelConfigDAL();
            if (ldal.Remove(lid))
            {
                response.Success = true;
                response.ExtraParams["msg"] = "删除成功！";
            }
            else
            {
                response.Success = false;
                response.ExtraParams["msg"] = "找不到数据。";
            }
            return response;
        }

        public AjaxStoreResult GetLevelCfgForm(int lid)
        {
            FM_LevelConfigDAL ldal = new FM_LevelConfigDAL();
            FM_LevelConfig lcfg = ldal.GetLevelCfg(lid);
            return new AjaxStoreResult(lcfg);
        }
    }
}
